package edu.uic.cs.i422.s2011.team4;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class SettingObservation extends Activity implements OnItemClickListener {	
	Toast mToastHandler;
	
	Integer[] mImages = {
            R.drawable.single_family, R.drawable.apartments,
            R.drawable.high_rise, R.drawable.residential_park,
            R.drawable.forest_park, R.drawable.botanical_gardens,
            R.drawable.comercial_establishments, R.drawable.school,
            R.drawable.vacant_lot, R.drawable.cemetery,
            R.drawable.farmland
    };
	Integer[] mStrings = {
    		R.string.settingItem1, R.string.settingItem2,
    		R.string.settingItem3, R.string.settingItem4,
    		R.string.settingItem5, R.string.settingItem6,
    		R.string.settingItem7, R.string.settingItem8,
    		R.string.settingItem9, R.string.settingItem10,
    		R.string.settingItem11
    };
	Integer[] mStringsHelp = {
			R.string.settingItem1Help, R.string.settingItem2Help,
    		R.string.settingItem3Help, R.string.settingItem4Help,
    		R.string.settingItem5Help, R.string.settingItem6Help,
    		R.string.settingItem7Help, R.string.settingItem8Help,
    		R.string.settingItem9Help, R.string.settingItem10Help,
    		R.string.settingItem11Help
    };
	
	SquirrelData.SettingType[] mDataSetting = {
			SquirrelData.SettingType.SINGLE_FAM, SquirrelData.SettingType.MULT_FAM,
			SquirrelData.SettingType.HIGHRISE, SquirrelData.SettingType.PARK,
			SquirrelData.SettingType.NATURE, SquirrelData.SettingType.GARDEN,
			SquirrelData.SettingType.COMMER, SquirrelData.SettingType.SCHOOL,
			SquirrelData.SettingType.LOT, SquirrelData.SettingType.CEMETERY,
			SquirrelData.SettingType.FARM
	};
	
	SquirrelData.SettingType mDataSettingCurrent;
	View mIndicatorCurrent = null;

	
	/** Called when the activity is first created. */    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        /**
         * Get our UI Generator for this Activity and set the title, help, and tree
         * Note that the strings should be R.string....
         */
        SquirrelUIGenerator squirrel = new SquirrelUIGenerator(this);
        squirrel.setTitle(R.string.settingTitle);
        squirrel.setHelp(R.string.settingHelp);
        squirrel.setBreadCrumb(R.string.settingBreadCrumb);
        
        /**
         * Create the encapsulating layout for this activity. We are going to add
         * every View we create here using addView().
         */
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL); //Set Orientation
        layout.setScrollContainer(true);
        
        /**
         * Bring forth my setting template
         */
        LinearLayout setting = (LinearLayout) View.inflate(this, R.layout.setting, null);
        
        /**
         * Point to grid and add the images and all the necessary functionality
         */
        GridView grid = (GridView) setting.findViewById(R.id.settingGrid);
        grid.setAdapter(new ItemsAdapter(this, mImages, mStrings));
        grid.setOnItemClickListener(this);
        
        Button done = (Button) setting.findViewById(R.id.settingDone);
        done.setOnClickListener(new OnClickListener() {            
            public void onClick(View v) {
            	Intent intent = new Intent(v.getContext(), TreeObservation.class);
				startActivityForResult(intent, 0);
            }
        });
        
        /**
         * This is how you should create the header. The title should be already have
         * been passed above. The function just returns the View which is created by
         * the SquirrelUIGenerator.
         */
        layout.addView(squirrel.getHeader());
        layout.addView(setting);
        
        /**
         * Finally we put our whole View into this Activity Content View, so we can
         * show it. 
         */
        setContentView(layout);
        
        /**
         * Welcome Message
         */
        mToastHandler = Toast.makeText(this, R.string.settingHelp, Toast.LENGTH_LONG);
        mToastHandler.show();
    }
    
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		/* Notify the user */
		mToastHandler.cancel();
		mToastHandler.setDuration(Toast.LENGTH_SHORT);
		mToastHandler.setText(mStringsHelp[position]);
		mToastHandler.show();
		
		/* Change Indicator, first time or the following times */
		if (mIndicatorCurrent == null) {
			/* get the new view Image Indicator */
			ImageView indicator = (ImageView) v.findViewById(R.id.settingItemIndicator);
			indicator.setImageResource(R.drawable.squirrel_yes);
			
			/* Save, so we can set up for later use */
			mIndicatorCurrent = v;
			
			/* Save the data */
			SquirrelData.setSettingType(mDataSetting[position]);
		} else if (mDataSettingCurrent != mDataSetting[position]) {
			/* get the current view Image indicator */
			ImageView lastIndicator = (ImageView) mIndicatorCurrent.findViewById(R.id.settingItemIndicator);
			lastIndicator.setImageResource(R.drawable.squirrel_dot);
			
			/* get the new view Image Indicator */
			ImageView indicator = (ImageView) v.findViewById(R.id.settingItemIndicator);
			indicator.setImageResource(R.drawable.squirrel_yes);
			
			/* Save, so we can set up for later use */
			mIndicatorCurrent = v;
			
			/* Save the data */
			SquirrelData.setSettingType(mDataSetting[position]);
		}
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
            	setResult(RESULT_OK);
                finish();
            }
        }
    }
}
